package com.sizu.web;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sizu.common.inventorydto.TGoodsDto;
import com.sizu.common.vo.ResultEntity;
import com.sizu.entity.TGoods;
import com.sizu.service.ITGoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 赵玉玺
 * @since 2020-03-14
 */
@RestController
@RequestMapping("/inventory/tGoods")
public class TGoodsController {

    @Resource
    private ITGoodsService itGoodsService;

    /**
     * 根据条件查询出商品列表
     *
     * @param tGoodsDto
     * @return
     */
    @RequestMapping("/list")
    public ResultEntity tGoodsList(TGoodsDto tGoodsDto) {
        PageHelper.startPage(tGoodsDto.getPageNum(), tGoodsDto.getPageSize());
        TGoods tGoods = new TGoods();
        BeanUtils.copyProperties(tGoodsDto, tGoods);
        Wrapper<TGoods> wrapper = new EntityWrapper<>();
        wrapper.ne("state",0);

        if (null != tGoodsDto.getTypeId() && tGoodsDto.getTypeId() != 0) {
            wrapper.eq("type_id", tGoodsDto.getTypeId());
        }
        if (null != tGoodsDto.getMh() &&!"".equals(tGoodsDto.getMh())) {
            wrapper.like("code", tGoodsDto.getMh()).or().like("name", tGoodsDto.getMh());
        }
        wrapper.like("name", tGoodsDto.getName());
        List<TGoods> tGoods1 = itGoodsService.selectList(wrapper);
        PageInfo<TGoods> pageInfo = new PageInfo<>(tGoods1);
        return ResultEntity.ok("查询成功", pageInfo);
    }

    /**
     * 修改库存数量
     * @param tGoodsDto
     * @return
     */
    @RequestMapping("/up")
    public ResultEntity tGoodsUp(TGoodsDto tGoodsDto){
        if(null!=tGoodsDto.getGoodsJson()&&!"".equals(tGoodsDto.getGoodsJson())){
            List<TGoods> tGoods = JSON.parseArray(tGoodsDto.getGoodsJson(), TGoods.class);
            for (TGoods tGood : tGoods) {
                itGoodsService.updateById(tGood);
            }
        }

        return ResultEntity.ok("修改成功");
    }

    /*
     * 查询库存报警的方法
     */
    @RequestMapping("/list1")
    public ResultEntity list1(){
        List<TGoods> list = itGoodsService.selectList1();
        return ResultEntity.ok(list);
    }

}
